⚡️ Speed up method TestEpsilonNash.setup_method by 6%
#21
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 6% (0.06x) speedup for
TestEpsilonNash.setup_methodinquantecon/game_theory/tests/test_mclennan_tourky.py⏱️ Runtime :
820 microseconds→772 microseconds(best of384runs)📝 Explanation and details
The optimized code achieves a 6% speedup through several targeted micro-optimizations:
1. Efficient Array Initialization: Replaced
np.empty()+ full assignment withnp.zeros()+ single assignment. The original code setspayoff_array[1, :] = 0then overwrites one element, while the optimized version directly sets onlypayoff_array[1, 0] = vsince zeros are already initialized.2. Computation Memoization: In
epsilon_nash_interval(), cached repeated calculations likev ** (1/(N-1))and(N-1)to avoid redundant mathematical operations across the nested function calls.3. Function Reference Caching: Stored local references to
anti_coordinationandepsilon_nash_intervaloutside the loop to eliminate repeated function name lookups during iteration.4. Reduced Object Construction: Moved bimatrix creation to a local variable before assignment, reducing intermediate object references.
The optimizations are most effective for the test cases involving multiple game creation (like
test_many_game_dictswith 18 games), where the cached computations and reduced array operations compound. For single-game scenarios, the benefit is minimal but still measurable. The 60% of runtime spent inanti_coordinationcalls (per profiler) makes the array initialization optimization particularly impactful.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-TestEpsilonNash.setup_method-mgh0nkcnand push.